package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 10:21
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private DtsCategoryService dtsCategoryService;
    // //ttp://localhost:8080/admin/category/list?page=1&limit=20&id=h&name=s&sort=add_time&order=desc
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="查找")
    @RequiresPermissions("admin:category:list")
    @GetMapping("/list")
    public  Object categoryList(Integer page,Integer limit,String sort,Integer id, String name,String order){

         Page<DtsCategory> dtsCategoryPage= dtsCategoryService.selectPage(page,limit,sort,id,name,order);
        List<DtsCategory> records = dtsCategoryPage.getRecords();
        long total = dtsCategoryPage.getTotal();


        HashMap<String, Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",records);
        return ResponseUtil.ok(data);

    }
    //http://localhost:8083/admin/category/l1
    @GetMapping("/l1")
    /**
     * 	"errno": 0,
     * 	"data": [
     *                {
     * 			"value": 1001002,
     * 			"label": "女装/男装"
     *        },
     *        {
     * 			"value": 1001006,
     * 			"label": "童装/母婴"
     *        }
     * 	],
     * 	"errmsg": "成功"
     * }
     */
    public  Object cateGryL1(){
        Set<CatVo> catVos=   dtsCategoryService.selectParentCatVo();

        return ResponseUtil.ok(catVos);
    }

    //http://localhost:8080/admin/category/create

    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="添加")
    @RequiresPermissions("admin:category:create")
    @PostMapping("/create")
    public  Object categoryCreate(@RequestBody DtsCategory dtsCategory){
        if(dtsCategory==null){
            return ResponseUtil.badArgumentValue();
        }
dtsCategory.setAddTime(new Date());
dtsCategory.setUpdateTime(new Date());
dtsCategory.setDeleted(false);
        dtsCategoryService.insert(dtsCategory);
        return ResponseUtil.ok(dtsCategory);
    }


    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="编辑")
    @RequiresPermissions("admin:category:update")
@PostMapping("/update")
    public  Object updateCategory(@RequestBody DtsCategory dtsCategory){
        if(dtsCategory==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsCategory.setUpdateTime(new Date());
        dtsCategoryService.update(dtsCategory);
        return ResponseUtil.ok(dtsCategory);
}

    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button ="删除")
    @RequiresPermissions("admin:category:delete")
@PostMapping("/delete")
    public  Object deleteCategory(@RequestBody DtsCategory dtsCategory){
if(dtsCategory==null){
    return ResponseUtil.badArgumentValue();
}
        dtsCategory.setDeleted(true);
        dtsCategoryService.delete(dtsCategory);
        return ResponseUtil.ok();
}
//http://localhost:8080/admin/order/list?page=1&limit=20&orderStatusArray=102&sort=add_time&order=desc&userId=s&orderSn=d
}
